import React, {Component} from 'react';
import '../css/successfuleCase.css';
import {Consumer} from "../store";
import {news} from "../value";
import app from '../appMethod';
import moment from 'moment';
import $ from "jquery";

class News extends Component {
    constructor(props) {
        super(props);
        this.state = {
            news_title: '',
            news_detail: '',
            created_at: '',
            detailContainer: ''
        };
    }

    render() {
        const {news_title, created_at} = this.state;
        return (
            <div style={{overflow: 'hidden'}}>
                <div className="newsHeader">
                </div>
                <div className={"newsList"}>
                    <div className={"normalContainer"}>
                        <div className={"newsDetailTitle"}>{news_title}</div>
                        <div className={"uptime"}>{created_at}<span
                            style={{fontWeight:'bold', fontSize: '14px', marginLeft: '40px'}}>F：领蓝信息科技</span></div>
                        <div id={"detailContainer"}
                             dangerouslySetInnerHTML={{__html: this.state.detailContainer}}></div>
                        <div className={"loadMore"} onClick={() => this.handleRedirect()}>返回</div>
                    </div>
                </div>
            </div>
        )
    };

    componentDidMount() {
        const {dispatch} = this.props;
        dispatch({num: 5});
        $(document).scrollTop(0);
        this.searchNewsById();
    };

    /**
     * 根据新闻Id查找新闻详情
     * @param param
     */
    searchNewsById() {
        let _this = this;
        const news_id = this.props.match.params.id;
        app.post(news.SEARCH_NEWS_BY_ID, {news_id: news_id}).then(result => {
            if (result.code === 1) {
                const data = result.data;
                //document.getElementById('detailContainer').innerHTML=data.news_detail;
                _this.setState({
                    news_title: data.news_title,
                    news_detail: data.news_detail,
                    created_at: moment(data.created_at).format('YYYY年MM月DD日'),
                    detailContainer:data.news_detail
                });
            } else {
                app.alert(result.msg, 'success', 2);
                _this.setState({news_title: '', news_detail: '', created_at: '',detailContainer:''});
            }
        });
    }

    /**
     * 处理返回事件
     * @param num
     */
    handleRedirect() {
        this.props.history.goBack();
    }

}

export default prop => (
    <Consumer>
        {
            props => (
                <News {...props} {...prop}/>
            )
        }
    </Consumer>
)
//export default News;